﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using BrainTechLLC;

namespace BrainTechLLC.FilterExtensions
{
    public partial class FilterCollectionCollection : ObservableCollection<FilterCollection>, INotifyPropertyChanged
    {
        protected FastSmartWeakEvent<EventHandler<NotifyCollectionChangedEventArgs>> _filterCollectionsChanged = new FastSmartWeakEvent<EventHandler<NotifyCollectionChangedEventArgs>>();
        public event EventHandler<NotifyCollectionChangedEventArgs> FilterCollectionsChanged { add { _filterCollectionsChanged.Add(value); } remove { _filterCollectionsChanged.Remove(value); } }

        protected FastSmartWeakEvent<EventHandler<ObjectEventArgs<FilterCollection>>> _singleFilterCollectionChanged = new FastSmartWeakEvent<EventHandler<ObjectEventArgs<FilterCollection>>>();
        public event EventHandler<ObjectEventArgs<FilterCollection>> SingleFilterCollectionChanged { add { _singleFilterCollectionChanged.Add(value); } remove { _singleFilterCollectionChanged.Remove(value); } }

        protected int _currentGeneration;
        protected object _lockObj = new object();
        
        public int CurrentGeneration
        {
            get { return _currentGeneration; }
        }

        public FilterCollectionCollection()
        {
        }

        public void SetCurrentGeneration(int newGeneration, NotifyCollectionChangedEventArgs e)
        {
            if (_currentGeneration != newGeneration)
            {
                _currentGeneration = newGeneration;
                _filterCollectionsChanged.Raise(this, e);

                OnPropertyChanged(new PropertyChangedEventArgs("CurrentGeneration"));
            }
        }

        public void GotoNextGeneration(NotifyCollectionChangedEventArgs e)
        {
            SetCurrentGeneration(CurrentGeneration + 1, e);
        }

        public void AddFilterCollection(FilterCollection filter)
        {
            if (filter != null)
            {
                filter.CollectionChanged += new NotifyCollectionChangedEventHandler(filter_CollectionChanged);
                filter.GenerationChanged += new EventHandler<NotifyCollectionChangedEventArgs>(filter_GenerationChanged);
                this.Add(filter);
                _filterCollectionsChanged.Raise(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, filter));
            }
        }

        private void filter_GenerationChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            _singleFilterCollectionChanged.Raise(this, new ObjectEventArgs<FilterCollection>(sender as FilterCollection));
        }

        private void filter_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            _singleFilterCollectionChanged.Raise(this, new ObjectEventArgs<FilterCollection>(sender as FilterCollection));
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        public void RemoveFilterCollection(FilterCollection filter)
        {
            filter.CollectionChanged -= filter_CollectionChanged;
            filter.GenerationChanged -= filter_GenerationChanged;
            this.Remove(filter);
            _filterCollectionsChanged.Raise(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, filter));
        }

    }
}
